import requests

# import urllib


# headers = {"Host": "http://110.42.175.97:8001"}

# post_url = 'http://110.42.175.97:8001/rcs/api/guest/648a1e0c53ececb7e1fc260ba5a47959/robot-control/control'

# request = {
# "robotid": 106,
# "command": "chas-forward 113.044 101.815 1.0 1",
# }

# get_url = "http://110.42.175.97:8001/rcs/api/guest/648a1e0c53ececb7e1fc260ba5a47959/robot-control/status?sceneid=189"

# response = requests.post(post_url,data=request)
# status  = requests.get(get_url,headers=headers)

# status2 = requests.request("GET", 'http://110.42.175.97:8001/rcs/api/guest/648a1e0c53ececb7e1fc260ba5a47959/robot-control/status?sceneid=189 HTTP/2.0',headers=headers)
# print(status2.text)
# print(response.text)
# print(status.text)


def control():
    url = 'http://110.42.175.97:8001/rcs/api/guest/648a1e0c53ececb7e1fc260ba5a47959/robot-control/control'
    payload = {
        "robotid": 107,
        "command": "chas-forward 113.044 101.815 1.0 1"
    }

    response = requests.post(url, json=payload)

    if response.status_code == 200:
        data = response.json()
        if data["ok"]:
            print("Operation successful")
        else:
            print("Operation failed. Error message: {}".format(data["msg"]))
    else:
        print("Failed to connect to the server")

control()

def status():
    url = 'http://110.42.175.97:8001/rcs/api/guest/648a1e0c53ececb7e1fc260ba5a47959/robot-control/status?sceneid=189'
    params = {
        "sceneid": 189
    }

    response = requests.get(url, params=params)

    if response.status_code == 200:
        data = response.json()
        if data["ok"]:
            status_list = data["statuslist"]
            for status in status_list:
                print("Robot ID: {}".format(status["robotId"]))
                print("Position X: {}".format(status["posX"]))
                print("Position Y: {}".format(status["posY"]))
                print("Linear Speed: {}".format(status["linearSpeed"]))
                print("Angular Speed: {}".format(status["angularSpeed"]))
                print("Chas Orientation: {}".format(status["chasOrientation"]))
                print("Board Orientation: {}".format(status["boardOrientation"]))
                print("Status Name: {}".format(status["statusName"]))
                print("Task ID: {}".format(status["taskid"]))
                print("\n")
        else:
            print("Operation failed. Error message: {}".format(data["msg"]))
    else:
        print("Failed to connect to the server")

base_url = 'http://110.42.175.97:8001/v1.1/web/rest/login/login-by-pwd'

# 设置登录时要提交的数据
payload = {
    'uid': 'fangzhen',
    'pwd': 'fangzhen240129',
    "secret":"dc3cbd98-adfa-40f6-a005-ffaf269b1926"
}


session = requests.Session()

# 发送登录请求
login_response = session.post(base_url, data=payload)

# 检查登录是否成功
if login_response.status_code == 200:
    # 登录成功后，可以继续发送GET请求获取网页信息
    page_url = 'http://110.42.175.97:8001/v1.1/web/rest/shelf/shelf_location_list?session=a2596e6e79d140ec8e1f7ce7da616276&timestamp=140&sign=qzdgOydCzlsZ0GnoA8P4oQ%3D%3D&sceneId=180'
    page_response = session.get(page_url)
    status()
    # 打印获取到的网页信息
    print(page_response.text)

    robot_url = 'http://110.42.175.97:8001/v1/web/scene-monitor/reload-robots'
    
    robot_response = session.post(robot_url)

else:
    print('登录失败')
